package db;

import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

import utils.Settings;

/**
 * Server for the Database. 
 */
public class DBServer {
	
	private static final int NUM_OF_PARAMS = 2;
	private static final int INDEX_PARAM_DB_PORT = 0;
	private static final int INDEX_PARAM_DB_PATH = 1;
	
	
	private static int dbPort;
	private static String dbPath;
	
	// Singleton instance of this server
	private static DBServer instance;
	
	public static DBServer getInstance () {
		if (instance == null)
			instance = new DBServer ();
		return instance;
	}
	
	public void run () {
		
		ServerSocket listenSocket = null;
		
		try {
			listenSocket = new ServerSocket (dbPort);
			System.out.println ("DBServer listening on port" + ": " + dbPort);
			
			while (true)
			{
				Socket clientSocket = listenSocket.accept();	
				ConnectionThreadDBToServer conn = new ConnectionThreadDBToServer (clientSocket);
	            conn.start();
			}
		}
		catch (BindException bindEx) {
			showBindExceptionMessage();
		}
		catch (SocketException socketEx) {
			showBindExceptionMessage();
		}
		catch (Exception genEx) {
			System.out.println("inside genEx");
		    genEx.printStackTrace();
		}
		finally {
			if (listenSocket != null)
			{
				try {
					System.out.println("inside finally\n");
					listenSocket.close();
				} catch (IOException e) {	
					e.printStackTrace();
				}
			}
		}
	}

	private void showBindExceptionMessage()
	{
		System.out.println ("Already running on port" + ": " + dbPort + ". Exiting...");
	}
	
	/*******************************************************************************************************
	* What to do For the database:
	 
	* 1. put the file you got in the mail in C:/rpsdb (his name should be "RPSDb.mdb")
	* 2. register your file by following the instruction in the file "Register DB to the system" in moodle - 
	*    under the name we are supposed to give (page 2 - upper left side) you should write "RPSDb". 
	 *******************************************************************************************************/
	public static void main (String [] args) {
	
		DBServer server = getInstance ();
			
		if (0 == args.length) {
			DBServer.dbPort = Settings.DB_PORT;
			DBServer.dbPath = Settings.DEFAULT_DB_PATH;
		}
		else if (args.length == NUM_OF_PARAMS) {
			DBServer.dbPort = Integer.parseInt(args[INDEX_PARAM_DB_PORT]);
			DBServer.dbPath = args[INDEX_PARAM_DB_PATH];
		}
		else
			System.out.println("wrong parameters");
	
		System.out.println ("------------------------------------------------------------------");
		System.out.println ("                RPS          DB        S E R V E R");
		System.out.println ("------------------------------------------------------------------");
		
		server.run ();
	}

	public static String getDBPath()
	{
		return dbPath;
	}	
}
